/**
 * 测试用例列表获取接口测试用例
 *
 * Created by snowingsea on 2020/03/13.
 */
const chai = require('chai');
const should = chai.should(); // eslint-disable-line no-unused-vars
chai.use(require('chai-http'));

const common = require('./../../common');
const Administrator = require('./../../../models/administrator');
const uri = require('./../uri');
const benchmarks = [require('./../../../benchmark/generate-tests/self_designed')];

module.exports = describe('/GET functions/:id/tests.json', function () {
  const administrator = new Administrator();
  let project = {};
  let functionId;
  const newUserIds = [];
  function url(functionId) {
    return `/functions/${functionId}/tests.json`;
  }

  before(function (done) {
    administrator.login()
      .then(() => administrator.createRandomTester())
      .then((tester) => {
        newUserIds.push(tester.userId);
        return tester.createRandomProject();
      })
      .then((res) => {
        project = res;
        done();
      })
      .catch(done);
  });
  after(function (done) {
    administrator.removeUsers(newUserIds)
      .then(() => {
        done();
      })
      .catch(done);
  });

  common.should.be.user(uri, url, 'get');
  common.should.be.tester(uri, url, 'get');

  // 正常发起请求
  benchmarks.forEach((benchmark, index) => {
    it(`it should ${index === 0 ? 'GET' : 'get'} tests of function in ${benchmark.name}`, function (done) {
      let projectVersion;
      let allTests;
      project.createRandomVersion()
        .then((res) => {
          projectVersion = res;
          return projectVersion.getAllTests(benchmark);
        })
        .then((res) => {
          functionId = res[0].functionId;
          allTests = res;
          return project.creator.login();
        })
        .then(() => {
          const functionIds = new Set();
          allTests.forEach(({ functionId }) => {
            functionIds.add(functionId);
          });
          const promises = [...functionIds].map((functionId) => {
            const { accessToken } = project.creator.token;
            return new Promise((resolve, reject) => { // eslint-disable-line no-unused-vars
              chai.request(uri)
                .get(url(functionId))
                .set('Authorization', `Bearer ${accessToken}`)
                .end((err, res) => {
                  res.should.have.status(200);
                  common.should.be.testCasesMessage(res.body);
                  resolve();
                });
            });
          });
          return Promise.all(promises);
        })
        .then(() => {
          done();
        })
        .catch(done);
    });
  });

  // 不可获取他人项目的测试用例
  it('it should fail to get tests when the project is not yours', function (done) {
    let newUser;
    administrator.createRandomTester()
      .then((tester) => {
        newUser = tester;
        newUserIds.push(tester.userId);
        return tester.getToken();
      })
      .then(() => {
        chai.request(uri)
          .get(url(functionId))
          .set('Authorization', `Bearer ${newUser.token.accessToken}`)
          .end((err, res) => {
            res.should.have.status(404);
            common.should.be.errorMessage(res.body);
            done();
          });
      })
      .catch(done);
  });

  // 不可获取不存在的项目文件的测试用例
  it('it should fail to get tests when the project is not exist', function (done) {
    new Promise((resolve) => {
      chai.request(uri)
        .get(url('000000000000000000000000'))
        .set('Authorization', `Bearer ${project.creator.token.accessToken}`)
        .end((err, res) => {
          res.should.have.status(404);
          common.should.be.errorMessage(res.body);
          resolve();
        });
    })
      .then(done)
      .catch(done);
  });
});
